package psp.linearRegression;

public class LinearRegressionController {

	private LinearRegressionFile linearRegressionFile = null;
	private LinearRegressionCalculation linearRegressionCalculation = null;
	
	
	/**
	 * Constructor that reads the numbers of the file, calculates its linear regression and displays to the user
	 * @param fileName Name of the file that will be read
	 */
	public LinearRegressionController(String fileName) {
		try {
			this.linearRegressionFile = new LinearRegressionFile(fileName);
		} catch (IncorrectNumberOfSetsException mtts) {
			System.err.println(mtts.getMessage());
			System.exit(0);
		}
		
		try {
			this.linearRegressionCalculation = new LinearRegressionCalculation(
				this.linearRegressionFile.getXNumbers(), this.linearRegressionFile.getYNumbers());
		} catch (DifferentNumberOfElementsException dnoe) {
			System.err.println(dnoe.getMessage());
			System.exit(0);
		}
		
		this.showLinearRegressionResults();
	}
	
	
	/**
	 * Displays in the screen the values calculated in the linear regression
	 */
	private void showLinearRegressionResults() {
		// Show the values of Beta 0 and Beta 1. After that, show the whole formula.
		System.out.println("The value of Beta0 is " + 
				this.linearRegressionCalculation.getBeta0());
		System.out.println("The value of Beta1 is " + 
				this.linearRegressionCalculation.getBeta1());
		
		System.out.println("y = " + this.linearRegressionCalculation.getBeta0() + 
				" + " + this.linearRegressionCalculation.getBeta1() + "*x");
	}
}